package com.bootdo.wx.service.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;
import com.bootdo.wx.dao.MemberAuthDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberAuthDO;
import com.bootdo.wx.domain.MemberDoctorDO;
import com.bootdo.wx.service.MemberAuthService;



@Service
public class MemberAuthServiceImpl implements MemberAuthService {
	@Autowired
	private MemberAuthDao memberAuthDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public MemberAuthDO get(String id){
		return memberAuthDao.get(id);
	}
	
	@Override
	public List<MemberAuthDO> list(Map<String, Object> map){
		return memberAuthDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberAuthDao.count(map);
	}
	
	@Override
	public int save(MemberAuthDO memberAuth){
		return memberAuthDao.save(memberAuth);
	}
	
	@Override
	public int update(MemberAuthDO memberAuth){
		return memberAuthDao.update(memberAuth);
	}
	
	@Override
	public int remove(String id){
		return memberAuthDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberAuthDao.batchRemove(ids);
	}

	@Override
	public R memberAuth(Map<String, Object> map) {
		try {
			MemberAuthDO memberAuth = memberAuthDao.findByUserId(map.get("userId").toString());
			if(memberAuth!=null&&memberAuth.getStatus()<2){
				return R.error("请勿重复认证");
			}else{
				MemberAuthDO memberAuth1 = new MemberAuthDO();
				BeanUtils.populate(memberAuth1, map);
				memberAuth1.setCaAt(new Date());
				memberAuth1.setDelFlag(0);
				memberAuth1.setId(UUIDUtils.getUUID());
				Integer level = memberUserDao.get(map.get("userId").toString()).getLevel();
				memberAuth1.setType(level);
				/*if(level==0){
					memberAuth1.setType(0);
				}else if(level==1){
					memberAuth1.setType(1);
				}else if(level==1){
					memberAuth1.setType(2);
				}*/
				memberAuth1.setStatus(0);
				memberAuthDao.save(memberAuth1);
				return R.ok().put("data", memberAuth1);
			}
		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
			return R.error();
		}
	}

	@Override
	public R getMemberAuthinfo(Map<String, Object> map) {
		MemberAuthDO memberAuth = memberAuthDao.findByUserId(map.get("userId").toString());
		return R.ok().put("data", memberAuth);
	}

	@Override
	public R shen(MemberAuthDO memberAuth) {
		memberAuthDao.update(memberAuth);
		return R.ok();
	}
	
}
